home *** CD-ROM | disk | FTP | other *** search
- package java.net;
-
- import java.io.ByteArrayOutputStream;
- import java.io.FileDescriptor;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- class PlainSocketImpl extends SocketImpl {
- private static final int SOCKS_PROTO_VERS = 4;
- private static final int SOCKS_REPLY_VERS = 4;
- private static final int COMMAND_CONNECT = 1;
- private static final int COMMAND_BIND = 2;
- private static final int REQUEST_GRANTED = 90;
- private static final int REQUEST_REJECTED = 91;
- private static final int REQUEST_REJECTED_NO_IDENTD = 92;
- private static final int REQUEST_REJECTED_DIFF_IDENTS = 93;
- public static final String socksServerProp = "socksProxyHost";
- public static final String socksPortProp = "socksProxyPort";
- public static final String socksDefaultPortStr = "1080";
-
- protected synchronized void create(boolean var1) throws IOException {
- super.fd = new FileDescriptor();
- this.socketCreate(var1);
- }
-
- protected void connect(String var1, int var2) throws UnknownHostException, IOException {
- Object var3 = null;
-
- try {
- InetAddress var4 = InetAddress.getByName(var1);
-
- try {
- if (System.getProperty("socksProxyHost") != null) {
- this.doSOCKSConnect(var4, var2);
- return;
- }
-
- this.doConnect(var4, var2);
- return;
- } catch (IOException var6) {
- var3 = var6;
- }
- } catch (UnknownHostException var7) {
- var3 = var7;
- }
-
- this.socketClose();
- throw var3;
- }
-
- protected void connect(InetAddress var1, int var2) throws IOException {
- super.port = var2;
- super.address = var1;
-
- try {
- if (System.getProperty("socksProxyHost") != null) {
- this.doSOCKSConnect(var1, var2);
- } else {
- this.doConnect(var1, var2);
- }
- } catch (IOException var4) {
- this.socketClose();
- throw var4;
- }
- }
-
- private void connectToAddress(InetAddress var1, int var2) throws IOException {
- if (System.getProperty("socksProxyHost") != null) {
- this.doSOCKSConnect(var1, var2);
- } else {
- this.doConnect(var1, var2);
- }
- }
-
- private void doSOCKSConnect(InetAddress var1, int var2) throws IOException {
- this.connectToSocksServer();
- this.sendSOCKSCommandPacket(1, var1, var2);
- int var3 = this.getSOCKSReply();
- switch (var3) {
- case 90:
- return;
- case 91:
- case 92:
- throw new SocketException("SOCKS server cannot conect to identd");
- case 93:
- throw new SocketException("User name does not match identd name");
- default:
- }
- }
-
- private int getSOCKSReply() throws IOException {
- InputStream var1 = this.getInputStream();
- byte[] var2 = new byte[8];
- if (var1.read(var2) != var2.length) {
- throw new SocketException("Malformed reply from SOCKS server");
- } else if (var2[0] != 0) {
- throw new SocketException("Malformed reply from SOCKS server");
- } else {
- return var2[1];
- }
- }
-
- private void connectToSocksServer() throws IOException {
- String var1 = System.getProperty("socksProxyHost");
- if (var1 != null) {
- InetAddress var2 = InetAddress.getByName(var1);
- String var3 = System.getProperty("socksProxyPort", "1080");
-
- int var4;
- try {
- var4 = Integer.parseInt(var3);
- } catch (Exception var5) {
- throw new SocketException("Bad port number format");
- }
-
- this.doConnect(var2, var4);
- }
- }
-
- private void doConnect(InetAddress var1, int var2) throws IOException {
- ProtocolException var3 = null;
- int var4 = 0;
-
- while(var4 < 3) {
- try {
- this.socketConnect(var1, var2);
- return;
- } catch (ProtocolException var6) {
- this.socketClose();
- super.fd = new FileDescriptor();
- this.socketCreate(true);
- var3 = var6;
- ++var4;
- } catch (IOException var7) {
- this.socketClose();
- throw var7;
- }
- }
-
- this.socketClose();
- throw var3;
- }
-
- private void sendSOCKSCommandPacket(int var1, InetAddress var2, int var3) throws IOException {
- byte[] var4 = this.makeCommandPacket(var1, var2, var3);
- OutputStream var5 = this.getOutputStream();
- var5.write(var4);
- }
-
- private byte[] makeCommandPacket(int var1, InetAddress var2, int var3) {
- ByteArrayOutputStream var4 = new ByteArrayOutputStream(9);
- var4.write(4);
- var4.write(var1);
- var4.write(var3 >> 8 & 255);
- var4.write(var3 & 255);
- byte[] var5 = var2.getAddress();
- var4.write(var5, 0, var5.length);
- String var6 = System.getProperty("user.name");
- byte[] var7 = new byte[var6.length()];
- var6.getBytes(0, var6.length(), var7, 0);
- var4.write(var7, 0, var7.length);
- var4.write(0);
- return var4.toByteArray();
- }
-
- private boolean usingSocks() {
- return System.getProperty("socksProxyHost") != null;
- }
-
- protected synchronized void bind(InetAddress var1, int var2) throws IOException {
- this.socketBind(var1, var2);
- }
-
- protected synchronized void listen(int var1) throws IOException {
- this.socketListen(var1);
- }
-
- protected synchronized void accept(SocketImpl var1) throws IOException {
- this.socketAccept(var1);
- }
-
- protected synchronized InputStream getInputStream() throws IOException {
- return new SocketInputStream(this);
- }
-
- protected synchronized OutputStream getOutputStream() throws IOException {
- return new SocketOutputStream(this);
- }
-
- protected synchronized int available() throws IOException {
- return this.socketAvailable();
- }
-
- protected synchronized void close() throws IOException {
- if (super.fd != null) {
- this.socketClose();
- }
-
- }
-
- protected synchronized void finalize() throws IOException {
- if (super.fd != null) {
- this.socketClose();
- }
-
- }
-
- private native void socketCreate(boolean var1) throws IOException;
-
- private native void socketConnect(InetAddress var1, int var2) throws IOException;
-
- private native void socketBind(InetAddress var1, int var2) throws IOException;
-
- private native void socketListen(int var1) throws IOException;
-
- private native void socketAccept(SocketImpl var1) throws IOException;
-
- private native int socketAvailable() throws IOException;
-
- private native void socketClose() throws IOException;
-
- static {
- System.loadLibrary("net");
- }
- }
-